import axios from "axios";

class Http {
  constructor(opts = {}) {
    opts.baseURL =
      process.env.NODE_ENV === "dev" ? "" : process.env.VUE_APP_HOST;
    opts.timeout = opts.timeout || 5000;

    //
   

    if (opts.headers) {
      opts.headers.aa = opts.headers.aa || "admin";
    } else {
      opts.headers = {
        aa: "admin",
      };
    }
    // 添加到成员属性
    this.opts = opts;
  }
  setHttp(opts = {}) {
      let options = { ...this.opts, ...opts };
      let myaxios = axios.create(options)
      myaxios.interceptors.request.use((config) => {
        let token = sessionStorage.getItem('token')?sessionStorage.getItem('token'):''
        config.headers.Authorization = token
        return config
    })
    return myaxios;
  }

  get(url, params = {}, opts = {}) {
    return this.setHttp(opts).get(url, { params });
  }
  post(url, params = {}, opts = {}) {
    return this.setHttp(opts).post(url, params);
  }
  put(url, params = {}, opts = {}) {
    return this.setHttp(opts).put(url, params);
  }
  del(url, params = {}, opts = {}) {
    return this.setHttp(opts).delete(url, {
      data:params
    });
  }
}

export default new Http();

// axios.interceptors.request.use((config) => {
//     return config
// })
// axios.interceptors.response.use((data) => {
//     return data
// })

// export default axios
